package com.yaopaikeji.mapper;

import com.yaopaikeji.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO user ( " +
            "                    login_id, " +
            "                    pass, " +
            "                    name, " +
            "                    email, " +
            "                    phone, " +
            "                    role_id, " +
            "                    is_active, " +
            "                    create_time, " +
            "                    last_modified_time, " +
            "                    last_login_time " +
            ") VALUES         ( " +
            "                    #{loginId}, " +
            "                    #{pass}, " +
            "                    #{name}, " +
            "                    #{email}, " +
            "                    #{phone}, " +
            "                    #{roleInfo.id}, " +
            "                    #{isActive}, " +
            "                    #{createTime,jdbcType=DATE}, " +
            "                    DEFAULT, " +
            "                    DEFAULT " +
            ");")
    @SelectKey(statement = "select last_insert_id()",keyProperty = "id",before = false,resultType = int.class)
    int saveUser(User user);

    @Update("<script> " +
            "     update user  " +
            "          <set> " +
            "               <if test='loginId != null'> " +
            "                    login_id = #{loginId}, " +
            "               </if> " +
            "               <if test='pass != null'> " +
            "                    pass = #{pass}, " +
            "               </if> " +
            "               <if test='name != null'> " +
            "                    name = #{name}, " +
            "               </if> " +
            "               <if test='email != null'> " +
            "                    email = #{email}, " +
            "               </if> " +
            "               <if test='phone != null'> " +
            "                    phone = #{phone}, " +
            "               </if> " +
            "               <if test='roleInfo != null'> " +
            "                    role_id = #{roleInfo.id}, " +
            "               </if> " +
            "               <if test='isActive != null'> " +
            "                    is_active = #{isActive}, " +
            "               </if> " +
            "               <if test='createTime != null'> " +
            "                    create_time = #{createTime}, " +
            "               </if> " +
            "               <if test='lastModifiedTime != null'> " +
            "                    last_modified_time = #{lastModifiedTime}, " +
            "               </if> " +
            "               <if test='lastLoginTime != null'> " +
            "                    last_login_time = #{lastLoginTime} " +
            "               </if> " +
            "          </set> " +
            "          where id = #{id}; " +
            "</script>")
    int updateUser(User user);

    @Select("select * from user where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey")),
            @Result(column = "id",property = "oweToEmailGroups",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findEmailGroups"))
    })
    User findByPrimaryKey(int id);

    @Select("select * from user")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey")),
            @Result(column = "id",property = "oweToEmailGroups",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findEmailGroups"))
    })
    List<User> findAll();

    @Select("select * from user where login_id = #{loginId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey")),
            @Result(column = "id",property = "oweToEmailGroups",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findEmailGroups"))
    })
    User findByLoginId(String loginId);


    @Select("select * from user where email = #{email}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey")),
            @Result(column = "id",property = "oweToEmailGroups",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findEmailGroups"))
    })
    User findByEmail(String email);

    @Select("select * from user where phone = #{phone}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey")),
            @Result(column = "id",property = "oweToEmailGroups",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findEmailGroups"))
    })
    User findByPhone(String phone);

    @Select("select count(login_id) from user where login_id = #{loginId}")
    boolean isLoginIdExists(String loginId);

    @Select("select count(email) from user where email = #{email}")
    boolean isEmailExists(String email);

    @Select("select count(phone) from user where phone = #{phone}")
    boolean isPhoneExists(String phone);

    @Update("update user set is_active = #{isActive},last_modified_time = #{lastModifiedTime} " +
            "where login_id = #{loginId}")
    int updateIsActiveByLoginId(User user);

    @Update("update user set is_active = #{isActive},last_modified_time = #{lastModifiedTime} " +
            "where id = #{id}")
    int updateIsActiveById(User user);

    @Delete("delete from user where id = #{id}")
    int deleteUserById(int id);

}
